package com.xuecheng.content.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xuecheng.commons.enums.ErrorCode;
import com.xuecheng.commons.model.vo.*;
import com.xuecheng.content.domain.CoursePub;
import com.xuecheng.content.domain.CourseWorkRec;
import com.xuecheng.content.domain.Teachplan;
import com.xuecheng.content.domain.TeachplanWork;
import com.xuecheng.content.mapper.CourseWorkRecMapper;
import com.xuecheng.content.service.CoursePubService;
import com.xuecheng.content.service.CourseWorkRecService;
import com.xuecheng.content.service.TeachplanService;
import com.xuecheng.content.service.TeachplanWorkService;
import com.xuecheng.teaching.api.TeachingClient;
import com.xuecheng.web.exception.BusinessException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@Service
public class CourseWorkRecServiceImpl extends ServiceImpl<CourseWorkRecMapper, CourseWorkRec> implements CourseWorkRecService {

    @Autowired
    private CoursePubService coursePubService;//课程发布

    @Autowired
    private TeachingClient teachingClient;//作业feign

    @Autowired
    private TeachplanService teachplanService;//课程计划

    @Autowired
    private TeachplanWorkService teachplanWorkService;//课程绑定作业

    //批阅详情-统计数据
    @Override
    public ResponseResult<WorkCountVo> getWorkContentInfo(Long courPubId) {
        //根据课程发布id获取课程信息
        CoursePub coursePub = coursePubService.getById(courPubId);
        WorkCountVo workCountVo = new WorkCountVo();
        workCountVo.setCoursePubId(coursePub.getId());//课程发布id
        workCountVo.setCourseId(coursePub.getCourseId());//课程id
        workCountVo.setContentName(coursePub.getName());//课程名
        ResponseResult<CourseWorkRecVo> responseResult = teachingClient.findIdByPubId(courPubId);
        if (responseResult.getCode()!=0){
            throw new BusinessException(ErrorCode.DATAERROR);
        }
        ResponseResult<Integer> responseResult_workNum = teachingClient.getGroupNum(courPubId);
        if (responseResult_workNum.getCode()!=0){
            throw new BusinessException(ErrorCode.DATAERROR);
        }
        //取出统计表
        CourseWorkRecVo courseWorkRecVo = responseResult.getData();
        //取出数据
        Integer courseIncludeWorkNums = responseResult_workNum.getData();
        workCountVo.setLearnerNumber(courseWorkRecVo.getAnswerNumber());//答题人数
        workCountVo.setCommitterNumber(courseWorkRecVo.getAnswerNumber());//提交人数
        workCountVo.setWorkNumber(courseWorkRecVo.getWorkNumber());//课程总题目数
        workCountVo.setCourseIncludeWorkNums(courseIncludeWorkNums);//课程包含的作业数
        workCountVo.setTobeReviewed(courseWorkRecVo.getTobeReviewed());//待批阅数
        //查询课程计划
        List<Teachplan> teachplanList = teachplanService.lambdaQuery().eq(Teachplan::getCourseId,courPubId).eq(Teachplan::getMediaType, "001004").list();
        //课程计划结果集
        List<WorkCountTeachplanVo> teachplanVoList = new ArrayList<>();
        WorkCountTeachplanVo teachplanVo = new WorkCountTeachplanVo();
        for (Teachplan teachplan : teachplanList) {
            teachplanVo.setTeachPlanId(teachplan.getId());//课程计划id
            teachplanVo.setPname(teachplan.getPname());//课程计划名
            teachplanVoList.add(teachplanVo);
        }
        workCountVo.setTeachPlan(teachplanVoList);//设置课程计划（作业）
        //作业结果集
        List<WorkCountWorkListVo> workVoList=new ArrayList<>();
        WorkCountWorkListVo workListVo = new WorkCountWorkListVo();
        //查询绑定的作业
        List<TeachplanWork> workList = teachplanWorkService.lambdaQuery().eq(TeachplanWork::getCourseId, courPubId).list();
        for (TeachplanWork work : workList) {
            workListVo.setWorkId(work.getId());//作业id
            workListVo.setWorkTitle(work.getWorkTitle());//作业标题
            workVoList.add(workListVo);
        }
        workCountVo.setWorkList(workVoList);//设置绑定的作业
        return ResponseResult.okResult(workCountVo);
    }


}
